package cn.org.eshow.service.impl;

import cn.org.eshow.bean.query.AddressQuery;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.dao.AddressDao;
import cn.org.eshow.model.Address;
import cn.org.eshow.model.User;
import cn.org.eshow.service.AddressManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.WebService;
import java.util.Date;
import java.util.List;

@WebService(serviceName = "AddressService", endpointInterface = "cn.org.eshow.service.AddressManager")
@Service
public class AddressManagerImpl extends GenericManagerImpl<Address, Integer> implements AddressManager {

    @Autowired
    AddressDao addressDao;

    public AddressManagerImpl() {
    }

    @Autowired
    public AddressManagerImpl(AddressDao addressDao) {
        super(addressDao);
        this.addressDao = addressDao;
    }

    @Override
    public List<Address> list(AddressQuery query) {
        return addressDao.list(query);
    }

    @Override
    public Page<Address> search(AddressQuery query) {
        return addressDao.search(query);
    }

    @Override
    public Address check(AddressQuery query) {
        List<Address> list = addressDao.list(query);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public Address save(Address address, User user) {
        if (address.getAddr() == null || address.getName() == null || address.getMobile() == null) {
            return null;
        }

        address.setUser(user);
        if (address.getZip() == null) {
            address.setZip("200000");
        }
        AddressQuery query = new AddressQuery();
        query.setUserId(user.getId());
        List<Address> addresses = addressDao.list(new AddressQuery(user.getId(),true));//查询用户地址列表
        if (addresses.isEmpty()) {//是否为第一次添加地址:第一个地址强制为默认地址(原型中,添加地址没有选择是否默认地址时 采用)
            address.setIfDefault(true);
            address = addressDao.save(address);//新增加
        } else {
            address.setIfDefault(false);//第二个以后的地址强制为非默认地址

            query.setMobile(address.getMobile());
            query.setName(address.getName());
            query.setAddr(address.getAddr());
            List<Address> list = addressDao.list(query);
            if (list.isEmpty()) {

                address = addressDao.save(address);//新增加
            } else {
                address = list.get(0);
            }
            if (address.getIfDefault().equals(Boolean.TRUE)) {
                //其他地址设置为非默认地址
                List<Address> objs = addressDao.list(new AddressQuery(Boolean.TRUE, user.getId()));//查询用户之前的默认地址
                for (Address obj : objs) {
                    if (obj.getId() != address.getId()) {
                        obj.setIfDefault(Boolean.FALSE);
                        addressDao.save(obj);
                    }
                }
            }
        }
        return address;
    }

    @Override
    public Address update(Address old, Address address, User user) {
        old.setUpdateTime(new Date());
        old.setAddr(address.getAddr() != null ? address.getAddr() : old.getAddr());
        old.setMobile(address.getMobile() != null ? address.getMobile() : old.getMobile());
        old.setZip(address.getZip() != null ? address.getZip() : old.getZip());
        old.setName(address.getName() != null ? address.getName() : old.getName());
        old.setProvince(address.getProvince() != null ? address.getProvince() : old.getProvince());
        old.setCity(address.getCity() != null ? address.getCity() : old.getCity());
        old.setDistrict(address.getDistrict() != null ? address.getDistrict() : old.getDistrict());
        old.setIfDefault(address.getIfDefault() != null ? address.getIfDefault() : old.getIfDefault());

        old.setEnabled(Boolean.TRUE);
        if (old.getIfDefault().equals(Boolean.TRUE)) {
            //其他地址设置为非默认地址
            List<Address> addresses = addressDao.list(new AddressQuery(Boolean.TRUE, user.getId()));//查询用户之前的默认地址
            for (Address obj : addresses) {
                if (obj.getId() != old.getId()) {
                    obj.setIfDefault(Boolean.FALSE);
                    addressDao.save(obj);
                }
            }
        }
        return addressDao.save(old);
    }

    @Override
    public Address enabled(Address old) {
        old.setUpdateTime(new Date());
        old.setEnabled(false);
        old=addressDao.save(old);
        if(old.getIfDefault()){//判断删除的地址是否为默认地址
            Address leftAddress = this.check(new AddressQuery(Boolean.FALSE,old.getUser().getId()));//查询用户某一个非默认地址
            if(leftAddress!=null){
                leftAddress.setIfDefault(true);
                leftAddress.setUpdateTime(new Date());
                leftAddress=addressDao.save(leftAddress);
            }
        }
        return old;
    }

}